home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / Secret Folder / Problem 08 / Secret.cp < prev    next >
Encoding:
Text File  |  1998-06-07  |  3.1 KB  |  138 lines  |  [TEXT/CWIE]

  1. #include "Solution.h"
  2.  
  3. #include "ProblemUtils.h"
  4.  
  5. #include <stdio.h>
  6. #include <Files.h>
  7. #include <Errors.h>
  8.  
  9. const int MAX_BOXES = 1000;
  10.  
  11. static OSErr ReadUInt32FromHandle( Handle data, UInt32 *number )
  12. {
  13.     OSErr err;
  14.     char line[MAX_LINE_LEN];
  15.     char *p;
  16.  
  17.     p = line;    
  18.     if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN ) && ProblemGetUInt32( &p, number ) && *p == 0 ) {
  19.         err = noErr;
  20.     } else {
  21.         err = -1;
  22.     }
  23.     return err;
  24. }
  25.  
  26. static OSErr ReadPointFromHandle( Handle data, Point *pt )
  27. {
  28.     OSErr err;
  29.     char line[MAX_LINE_LEN];
  30.     char *p;
  31.     UInt32 number1, number2;
  32.  
  33.     p = line;    
  34.     if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN )
  35.             && ProblemGetUInt32( &p, &number1 )
  36.             && ProblemGetUInt32( &p, &number2 ) && *p == 0 ) {
  37.         pt->v = number1;
  38.         pt->h = number2;
  39.         err = noErr;
  40.     } else {
  41.         err = -1;
  42.     }
  43.     return err;
  44. }
  45.  
  46. static OSErr ReadRectFromHandle( Handle data, Rect *r )
  47. {
  48.     OSErr err;
  49.     char line[MAX_LINE_LEN];
  50.     char *p;
  51.     UInt32 number1, number2, number3, number4;
  52.  
  53.     p = line;    
  54.     if ( ProblemReadLineFromHandle( data, line, MAX_LINE_LEN )
  55.             && ProblemGetUInt32( &p, &number1 )
  56.             && ProblemGetUInt32( &p, &number2 )
  57.             && ProblemGetUInt32( &p, &number3 )
  58.             && ProblemGetUInt32( &p, &number4 ) && *p == 0 ) {
  59.         r->top = number1;
  60.         r->left = number2;
  61.         r->bottom = number3;
  62.         r->right = number4;
  63.         err = noErr;
  64.     } else {
  65.         err = -1;
  66.     }
  67.     return err;
  68. }
  69.  
  70. pascal OSErr CheckPackBoxes( const FSSpec* infile, const FSSpec* outfile, Boolean *correct )
  71. {
  72.     OSErr err;
  73.     Handle indata;
  74.     UInt32 box_count;
  75.     UInt32 i, j;
  76.     Rect frame;
  77.     Point boxes[MAX_BOXES];
  78.     Point boxes_saved[MAX_BOXES];
  79.     Point toplefts[MAX_BOXES];
  80.     
  81.     *correct = false;
  82.     
  83.     err = ProblemFileRead( infile, &indata );
  84.     if ( err == noErr ) {
  85.         err = ReadRectFromHandle( indata, &frame );
  86.         if ( err == noErr ) {
  87.             err = ReadUInt32FromHandle( indata, &box_count );
  88.         }
  89.         if ( err == noErr ) {
  90.             for ( i = 0; i < box_count; i++ ) {
  91.                 err = ReadPointFromHandle( indata, &boxes[i] );
  92.                 boxes_saved[i] = boxes[i];
  93.                 if ( err != noErr ) break;
  94.             }
  95.         }
  96.         
  97.         if ( err == noErr ) {
  98.             PackBoxes( &frame, box_count, boxes, toplefts );
  99.             *correct = true;
  100.             for ( i = 0; i < box_count && *correct; i++ ) {
  101.                 Rect thisbox;
  102.                 if ( boxes_saved[i].h != boxes[i].h || boxes_saved[i].v != boxes[i].v ) {
  103.                     *correct = false;
  104.                     break;
  105.                 }
  106.                 thisbox.left = toplefts[i].h;
  107.                 thisbox.top = toplefts[i].v;
  108.                 thisbox.right = thisbox.left + boxes[i].h;
  109.                 thisbox.bottom = thisbox.top + boxes[i].v;
  110.                 if ( thisbox.left < frame.left || thisbox.top < frame.top 
  111.                         || thisbox.right > frame.right || thisbox.bottom > frame.bottom ) {
  112.                     *correct = false;
  113.                     break;
  114.                 }
  115.                 for ( j = 0; j < box_count; j++ ) {
  116.                     if ( i != j ) {
  117.                         Rect thatbox;
  118.                         thatbox.left = toplefts[j].h;
  119.                         thatbox.top = toplefts[j].v;
  120.                         thatbox.right = thisbox.left + boxes[j].h;
  121.                         thatbox.bottom = thisbox.top + boxes[j].v;    
  122.                         if ( thatbox.right <= thisbox.left
  123.                                 || thatbox.left >= thisbox.right
  124.                                 || thatbox.bottom <= thisbox.top
  125.                                 || thatbox.top >= thisbox.bottom ) {
  126.                         } else {
  127.                             *correct = false;
  128.                             break;
  129.                         }
  130.                     }
  131.                 }
  132.             }
  133.         }
  134.         DisposeHandle( indata );
  135.     }
  136.     return err;
  137. }
  138.